package com.jiyun.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.entity.Order;
import com.jiyun.entity.User;
import com.jiyun.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  控制层
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-03-28
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    IOrderService orderService;

    /**
     * 添加接口
     */
    @RequestMapping("/add")
    public Integer addOrder(@RequestBody Order order, HttpSession session){
        if (session.getAttribute("user")==null) {
            return 300;
        }
        User user = JSON.parseObject(session.getAttribute("user")+"", User.class);
        if (user==null) {
            return 300;
        }
        order.setUid(user.getId());
        order.setCreateTime(LocalDateTime.now());
        return orderService.add(order);

    }

    /**
     * 修改接口
     */
    @RequestMapping("/update")
    public void updateOrder(@RequestBody Order order){
        orderService.updateById(order);
    }

    /**
     * 单个删除接口
     */
    @RequestMapping("/deleteOne")
    public void deleteOne(@RequestParam Integer id){
        orderService.removeById(id);
    }

    /**
     * 批量删除接口
     * 前台ajax参数ids传入示例：1,2,3  (英文逗号分隔的字符串)
     */
    @RequestMapping("/deleteBatch")
    public void deleteBatch(@RequestParam List<Integer> ids){
        orderService.removeByIds(ids);
    }

    /**
     * 根据id查询实体接口
     */
    @RequestMapping("/findById")
    public Order findById(@RequestParam Integer id){
        return orderService.getById(id);
    }

    /**
     * 查询所有接口
     */
    @RequestMapping("/findAll")
    public List<Order> findAll(){
        return orderService.list();
    }

    /**
     * 分页查询接口
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     */
    @RequestMapping("/findPage")
    public Page<Order> findPage(@RequestParam Integer page,@RequestParam Integer pageSize){
        Page<Order> orderPage = new Page<>(page, pageSize);
        return orderService.page(orderPage);
    }

}
